package com.example.fw;

import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.tests.GroupData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase{

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	
	public ContactHelper createContact(ContactData contact, boolean CREATION) {
		  manager.navigateTo().mainPage();
	      initContactCreation();
	      fillContactForm(contact, CREATION);
	      submitContactCreation();
	      returnToContactPage();
	      manager.getModel().addContact(contact);
	      return this;
	}

	public ContactHelper modifyContact(ContactData contact, int index, boolean MODIFICATION) {
		 manager.navigateTo().mainPage(); 
		 initModifyContract(index);
		 fillContactForm(contact, MODIFICATION);
	     submitModifyContact();
	     returnToContactPage();
		 manager.getModel().removeContact(index).addContact(contact);
	     return this;
	}
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		initModifyContract(index);
		submitContactRemove();
		returnToContactPage();
	    manager.getModel().removeContact(index);
		return this;
		
	}
	
	//--------------------------------------------------------
	public SortedListOf<ContactData> getUiContact() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		WebElement table = driver.findElement(By.id("maintable"));
		List<WebElement> tableRecords = table.findElements(By.xpath("//tr[@name='entry']"));
	        for(WebElement firstNameElement : tableRecords)
	        {
	        	WebElement firstname = firstNameElement.findElement(By.xpath("td[3]"));
	            String firstNameText = firstname.getText();
	            contacts.add(new ContactData().withFirstName(firstNameText));
	        }
	        return contacts;
	}
	
	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getTelephonehome());
		type(By.name("mobile"), contact.getTelephonemobil());
		type(By.name("work"), contact.getTelephonework());
		type(By.name("email"), contact.getEmail1());
		type(By.name("email2"), contact.getEmail2());
	    selectElement(By.name("bday"), contact.getBday());
	    selectElement(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		if (formType == CREATION){
	      selectElement(By.name("new_group"), contact.getGroupname());
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0){
				throw new Error("Group selector exists in contact modification from");
			}		
		}	
		type(By.name("address2"), contact.getAddress2());	    
		type(By.name("phone2"), contact.getHome2());	 
		return this;
	}


	private ContactHelper returnToContactPage() {
		 click(By.linkText("home page"));
		 return this;
	}
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper initModifyContract(int index) {
		click(By.xpath(".//*[@id='maintable']/tbody/tr[" + (index+2) + "]/td[7]/a/img"));
		return this;
	}

	public ContactHelper submitModifyContact() {
		click(By.name("update"));
		return this;
	}
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	private void submitContactRemove() {
		click(By.xpath("//input[@value='Delete']"));
	}
	
	public static String getRandomGroupUI() {
		List<WebElement> groups = driver.findElements(By.xpath(".//*[@id='content']/form/select[3]//option"));
	    Random rnd = new Random();
	    int index = rnd.nextInt(groups.size()+1);
	    WebElement group = driver.findElement(By.xpath(".//*[@id='content']/form/select[3]//option[" + index+ "]"));
	    return group.getText();

	}
	
	public static String getRandomGroupModel() {
		List<WebElement> groups = driver.findElements(By.xpath(".//*[@id='content']/form/select[3]//option"));
	    Random rnd = new Random();
	    int index = rnd.nextInt(groups.size()+1);
	    WebElement group = driver.findElement(By.xpath(".//*[@id='content']/form/select[3]//option[" + index+ "]"));
	    return group.getText();

	}
	
}
